package org.consumer.proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

import org.consumer.client.Client;
import org.consumer.rpcrequest.RpcRequest;

/**
 * rpc调用代理
 * 
 * @author Typhoon
 *
 */
public class RpcProxy {
	
	
	/**
	 * 生成代理类
	 * 
	 * @param clazz
	 * @param host
	 * @param port
	 * @return
	 */
	public static Object getRpcProxy(final Class clazz,final String host,final int port) {
		
		return Proxy.newProxyInstance(clazz.getClassLoader(), new Class[]{clazz}, new InvocationHandler() {
			public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
				RpcRequest request = new RpcRequest(clazz.getName(), method.getName(), method.getParameterTypes(), args);
				return Client.connectAndGet(host, port, request);
			}
		});
		
	}

}
